import 'package:baseProjectFlutter/utils/coroutine/coroutine.dart';
import 'package:get/get.dart';
import 'package:get/get_common/get_reset.dart';
import 'package:baseProjectFlutter/base/mvvm/base_view_model.dart';
import 'package:baseProjectFlutter/utils/other/log/log.dart';

import '../../utils/timer_extends.dart';

class HomeVm extends BaseViewModel {
  HomeVm();

  var count = 0.obs; // 使用 .obs 使变量可观察
  List<HomeItemData> mainData = [
    HomeItemData(1, "跳转登录"),
    HomeItemData(2, "指令测试"),
  ];

  @override
  void onCreate() {
    super.onCreate();
    count.value = 20;
    LogUtils.i("HomeVm 我创建了 $count,,, ${hashCode}");
  }

  Job? taskJob;
  final TaskManager _taskManager = TaskManager();

  void incrementCounter() {
    count.value++;
    Future aaa(Job job) async {
      await delayFuture(4000);
    }

    _taskManager.cancel();
    final isARun = taskJob == null;
    if (taskJob != null) {
      taskJob?.cancel();
      taskJob = null;
    }
    if (isARun) {
      taskJob = launchSJ((scope, job) async {
        while (job.isActive) {
          print("111111111111 run");
          await job.withCancel(delayFuture(4000));
          //走异步，不影响统计时间
          _taskManager.run(() async {
            print("2222222 run");
            await delayFuture(2000);
            print("2222222 end");
          });
          // await job.withCancel(aaa(job));
          print("111111111111  end");
        }
      });
    }
    //
    // taskLaunch(() async {
    //   print("222222222 run");
    //   await delayFuture(2000);
    //   print("222222222  end");
    //   taskJob?.cancel();
    // });
    // taskLaunch(() async {
    //   print("3333333333 run");
    //   await delayFuture(6000);
    //   print("3333333333 end");
    // });
    // print('End');
  }
}

class HomeItemData {
  String title;
  int type;

  HomeItemData(
    this.type,
    this.title,
  );
}
